package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.admin.CouponService;
import com.cskaoyan.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author by yxy_8175@163.com
 * @Description TODO
 * @Date 2022-11-19 17:43
 */

@RestController
@RequestMapping("admin/coupon")
public class CouponController {


    @Autowired
    CouponService couponService;

    /**
     * 优惠券列表展示
     *
     * @param info
     * @return
     * @auto yxy_8175@163.com 闫翔宇
     */
    @RequestMapping("list")
    public BaseRespVo showCoupons(BasePageInfo info, String name, Integer type, Integer status) {

        CommonData<MarketCoupon> commonData = couponService.selectByPage(info, name, type, status);

        return BaseRespVo.ok(commonData);
    }

    /**
     * 根据主键id来查询详细信息
     *
     * @param id
     * @return
     */
    @RequestMapping("read")
    public BaseRespVo queryCouponInfo(Integer id) {
        MarketCoupon marketCoupon = couponService.queryCouponInfoById(id);

        return BaseRespVo.ok(marketCoupon);
    }

    /**
     * 创建一条优惠券数据
     *
     * @param coupon
     * @return
     * @auto yxy_8175@163.com
     */
    @RequestMapping("create")
    public BaseRespVo createCoupon(@RequestBody MarketCoupon coupon) {

        MarketCoupon resultCoupon = couponService.createCoupon(coupon);

        return BaseRespVo.ok(resultCoupon);
    }

    private Object validate(MarketCoupon coupon){
        String name = coupon.getName();
        if (StringUtil.isEmpty(name)){
            return "更新优惠券不存在";
        }
        return null;
    }

    /**
     * 更新优惠券数据
     *
     * @param coupon 修改后的数据
     * @return
     * @auto yxy_8175@163.com
     */
    @RequestMapping("update")
    public BaseRespVo updateCouponInfo(@RequestBody MarketCoupon coupon) {
        // 加一层validate
        Object error = validate(coupon);
        if (error != null) {
            return BaseRespVo.error(404, error.toString());
        }

        MarketCoupon marketCoupon = couponService.updateCouponInfo(coupon);

        return BaseRespVo.ok(marketCoupon);
    }

    /**
     * 删除优惠券
     *
     * @param coupon 要删除的coupon
     * @return
     * @auto yxy_8175@163.com
     */
    @RequestMapping("delete")
    public BaseRespVo deleteCouponInfo(@RequestBody MarketCoupon coupon) {

        int status = couponService.deleteCoupon(coupon);

        if (status == BaseRespVo.STATUS_OK) {
            return BaseRespVo.ok();
        }

        return BaseRespVo.invalidData("删除失败...");
    }

    /**
     * 查询用户已有的对应状态的优惠券
     * @param info
     * @param couponId
     * @return
     */
    @RequestMapping("listuser")
    public BaseRespVo showUserByCoupon(BasePageInfo info, Integer couponId,Integer userId,Integer status) {

        CommonData commonData = couponService.queryUserFromCoupon(info,couponId,userId,status);

        return BaseRespVo.ok(commonData);
    }
}
